// 
package Leetcode_Second_Brush;

public class Solution110 {
    public boolean isBalanced(TreeNode root) {
        return getDepth(root) >= 0;
    }

    private int getDepth(TreeNode root) {
        if (root == null)
            return 0;

        int leftDepth = getDepth(root.left);
        int rightDepth = getDepth(root.right);

        // -1 表示该子树不平衡，则直接返回-1，表示false
        if (leftDepth == -1 || rightDepth == -1 || Math.abs(leftDepth - rightDepth) > 1)
            return -1;
        else
            return Math.max(leftDepth, rightDepth) + 1;
    }
}
